import java.util.Stack;

class Solution1 {
    public boolean isValid(String s) {
         Stack<Character> stack1 = new Stack<>();
        for(int i = 0; i < s.length();i++) {
            char ch = s.charAt(i);
            //左括号进栈
            if(ch == '(' || ch == '[' || ch == '{') {
                stack1.push(ch);
            }else {//字符未遍历完，但栈空
                if(stack1.empty()) {
                    return false;
                }
                char ch2 = stack1.peek();
                if(ch2 == '[' && ch == ']'
                || ch2 == '(' && ch == ')' 
                || ch2 == '{' && ch == '}') {
                    stack1.pop();
                }else{
                    return false;
                }
            }
        }
        if(!stack1.empty()){
            return false;
        }
        return true;
    }
  }